# coding:utf-8
from urllib.parse import urlencode
from pyquery import PyQuery as pq
import requests
import time
'''
    微博信息获取，以json格式保存到本地txt文件
'''
# 设置基础链接和信息
base_url = 'https://m.weibo.cn/api/container/getIndex?'
headers = {
    'Host': 'm.weibo.cn',
    'Referer': 'https://m.weibo.cn/u/5902995241',
    'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1',
    'X-Requested-With': 'XMLHttpRequest'
}
# 获取页面信息
def get_page(page):
    params = {
        'type': 'uid',
        'value': '5902995241',
        'containerid': '1076035902995241',
        'page': page
    }
    url = base_url + urlencode(params)
    try:
        response = requests.get(url, headers=headers)
        if response.status_code == 200:
            return response.json()
    except requests.ConnectionError as e:
        print('Error', e.args)

# 从json数据中提取需要的数据
def parse_page(jsonData):
    if jsonData:
        items = jsonData.get('data').get('cards')
        for item in items:
            blog = item.get('mblog')
            if blog:
                weibo = {}
                weibo['id'] = blog.get('id')
                weibo['text'] = pq(blog.get('text')).text()
                weibo['attitudes'] = blog.get('attitudes_count')
                weibo['comments'] = blog.get('comments_count')
                weibo['reposts'] = blog.get('reposts_count')
                yield weibo


if __name__ == '__main__':
    for page in range(1, 10):
        jsonData = get_page(page)
        datas = parse_page(jsonData)
        for data in datas:
            print('写入信息：' + str(data))
            with open('weibo.txt', 'a', encoding='utf-8') as f:
                f.write(str(data) + '\n')
                f.write('\n' + '='*50 + '\n')
        time.sleep(1)
